// pages/my-pages/staff/staff.js
const httpUtils = require('../../../utils/httpUtils')
const ui = require('../../../utils/ui')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: '',
    actionText: '查询',
    userList: [],
    total: 0, //分页总数
    page: 1, //分页记录数
    pageSize: 10, //分页大小
    onRefresh: true,
    positionVisible: false,
    dicPositionList: [],
    item: {},
    showTextAndTitleWithInput: false,
    showTextAndTitleWithAdd: false,
    myBindCode: '',
    shareUserCode: ''
  },

  //点击了左滑动删除按钮
  onDelete: function (e) {
    let item = e.currentTarget.dataset.entity;
    this.setData({
      ['showTextAndTitleWithInput']: true,
      item: item
    })
  },

  //点击删除弹窗的确定
  confirmDelShare: function (e) {
    let that = this;
    let item = that.data.item;
    let obj = {
      method: 'GET',
      showLoading: false,
      url: 'shareRelation/delBind?shareUserId=' + item.id
    }

    httpUtils.request(obj).then(res => {
      console.log(res.data.message)
      if (res.data.retCode == 0) {
        that.setData({
          showTextAndTitleWithInput: false,
          positionVisible: false,
          item: {},
          onRefresh: false,
          page: 1,
          userList: []
        });
        that.getMyShareFunc();
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },

  //点击删除弹窗的取消
  closeDialog: function () {
    this.setData({
      ['showTextAndTitleWithInput']: false
    });
  },

  //打开关系弹窗
  onPositionPicker: function (e) {
    let that = this;
    let item = e.currentTarget.dataset.entity;
    console.log(item);
    that.setData({
      positionVisible: true,
      item: item
    });
  },

  //关系弹窗点击确定按钮
  onPickerChange: function (e) {
    let that = this;
    const {
      key
    } = e.currentTarget.dataset;
    const {
      value
    } = e.detail;

    let obj = {
      method: 'POST',
      showLoading: false,
      url: 'shareRelation/updateRelationDic',
      data: {
        'shareUserId': key,
        'dicValue': value[0]
      }
    }

    httpUtils.request(obj).then(res => {
      console.log(res.data.message)
      if (res.data.retCode == 0) {
        that.setData({
          positionVisible: false,
          item: {},
          onRefresh: false,
          page: 1,
          userList: []
        });
        that.getMyShareFunc();
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },

  //打开新增弹窗
  openAddDialog: function (e) {
    let that = this;
    //获取绑定码
    let obj = {
      method: 'GET',
      showLoading: false,
      url: 'shareRelation/getShareCode',
    }
    httpUtils.request(obj).then(res => {
      console.log(res.data.message)
      if (res.data.retCode == 0) {
        that.setData({
          ['showTextAndTitleWithAdd']: true,
          myBindCode: res.data.data,
          shareUserCode: ''
        });
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },
  //新增弹窗数据改变
  inputBingShareCode: function (e) {
    this.setData({
      shareUserCode: e.detail.value
    })
  },
  //关闭新增弹窗
  closeAddDialog: function () {
    this.setData({
      ['showTextAndTitleWithAdd']: false,
      myBindCode: '',
      shareUserCode: ''
    });
  },

  //新增弹窗点击确定
  confirmAddShare: function () {
    let that = this;
    let url = 'shareRelation/bindShare?shareCode=' + that.data.shareUserCode;
    let obj = {
      method: 'GET',
      showLoading: false,
      url: url,
    }
    httpUtils.request(obj).then(res => {
      console.log(res.data.message)
      if (res.data.retCode == 0) {
        this.setData({
          onRefresh: false,
          showTextAndTitleWithAdd: false,
          myBindCode: '',
          shareUserCode: ''
        })
        this.getMyShareFunc();
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
    const {
      dialogKey
    } = this.data;
    this.setData({
      [dialogKey]: false
    });
  },

  //关系弹窗点击取消按钮
  onPickerCancel: function (e) {
    this.setData({
      positionVisible: false,
      item: {}
    })
  },

  changeHandle: function (e) {
    this.setData({
      value: e.detail.value
    })
  },

  clearHandle: function (e) {
    console.log('clearHandle' + e.detail.value)
    this.setData({
      value: ''
    });
  },

  actionHandle: function () {
    this.setData({
      onRefresh: false,
      page: 1,
      userList: []
    });
    this.getMyShareFunc();
  },

  getDicList: function () {
    let obj = {
      method: 'GET',
      showLoading: false,
      url: 'dic/miniProjectDicList?parentId=1777877567770038274'
    }
    httpUtils.request(obj).then(res => {
      console.log(res.data.message)
      if (res.data.retCode == 0) {
        this.setData({
          dicPositionList: res.data.data
        })
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },
  //查询共享用户列表
  getMyShareFunc: function () {
    let that = this;
    var onRefresh = that.data.onRefresh; //false为重新刷新数据，true为分页累加数据
    let obj = {
      method: 'POST',
      showLoading: false,
      url: 'shareRelation/getSharePage',
      data: {
        'nickName': that.data.value,
        'page': that.data.page,
        'size': that.data.pageSize
      }
    }
    httpUtils.request(obj).then(res => {
      console.log(res.data.message)
      if (res.data.retCode == 0) {
        var datas = res.data.data.records;
        // console.log('用户信息: ' + res.data.data)
        if (datas != null && datas.length > 0) {
          if (onRefresh) { //false为重新刷新数据，true为分页累加数据
            that.setData({
              userList: that.data.userList.concat(datas),
              page: res.data.data.current,
              total: res.data.data.pages
            })
          } else {
            that.setData({
              userList: datas,
              page: res.data.data.current,
              total: res.data.data.pages
            })
          }
        } else {
          ui.showToast('没有更多数据了')
        }
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败')
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getMyShareFunc();
    this.getDicList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      onRefresh: false
    });
    this.getMyShareFunc();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let that = this;
    this.setData({
      onRefresh: true,
      page: that.data.page + 1
    });
    this.getMyShareFunc();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})